from django.http import HttpResponse

from accounts.models import User


def ip_middleware(get_reponse):

    def middleware(request):
        # 请求到达之前的业务逻辑
        print('请求之前的业务逻辑')
        # 请求不满足  ip 被限制
        ip = request.META.get('REMOTE_ADDR', None)
        print(ip)
        ip_disable_list = [
            '127.0.0.1'
        ]
        if ip in ip_disable_list:
            return HttpResponse('not allowed', status=403)

        reponse = get_reponse(request)

        # 在视图函数调用之后的业务逻辑
        print('请求之后的业务逻辑')
        return reponse

    return middleware


class MallAuthMiddleware(object):
    '''自定义登录验证中间件'''

    def __init__(self, get_reponse):
        self.get_reponse = get_reponse

    def __call__(self, request, *args, **kwargs):
        print(' MallAuthMiddleware请求到达之前的业务逻辑')
        user_id = request.session.get('user_id', None)
        if user_id:
            user = User.objects.get(pk=user_id)
        else:
            user = None

        request.my_user = user
            
            
        reponse = self.get_reponse(request)

        print(' MallAuthMiddleware请求到达之后的业务逻辑')

        return reponse
